package cn.anjubao.cp.api.controller;

import cn.anjubao.cp.api.dto.*;
import cn.anjubao.cp.api.service.UserService;
import cn.anjubao.cp.common.base.BaseResponse;
import cn.anjubao.cp.common.base.exception.BusinessException;
import com.anjubao.parking.municipal.common.dto.ApiResult;
import com.anjubao.parking.municipal.user.dto.CaptchaDto;
import com.anjubao.parking.municipal.user.dto.UserBalanceRecordDto;
import com.anjubao.parking.municipal.user.dto.UserDetailDto;
import com.anjubao.parking.municipal.user.dto.UserVehicleDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    /**
     * 获取手机验证码
     * @param phone
     * @return
     */
    @GetMapping("/getCaptcha")
    public BaseResponse<CaptchaDto> getCaptcha(@RequestParam("phone") String phone) throws BusinessException{
       return BaseResponse.success( userService.getCaptcha(phone));
    }

    /**
     * 用户授权，更新用户信息
     * @param reqForm
     * @return
     */
    @PostMapping("/authorize")
    public BaseResponse<UserDetailDto> userAuthorize(@Validated @RequestBody UserAuthorizeReqFrom reqForm) throws BusinessException{
        logger.info("【用户授权】参数:{}",reqForm);
        return BaseResponse.success(userService.authorize(reqForm));
    }

    /**
     * 绑定车牌
     * @param reqForm
     * @return
     * @throws BusinessException
     */
    @PostMapping("/bindCarNo")
    public BaseResponse bindCarNo(@Validated @RequestBody BindCarNoReqForm reqForm) throws BusinessException {
        logger.info("【绑定用户车牌】参数:{}",reqForm);
        userService.bindCarNo(reqForm);
        return BaseResponse.success();
    }

    /**
     * 对比用户余额
     * @param reqForm
     * @return
     */
    @PostMapping("/checkBalance")
    public BaseResponse<BalanceResDto> checkBalance(@Validated @RequestBody BalanceReqForm reqForm) throws BusinessException{
        logger.info("【对比用户余额】参数:{}",reqForm);
        return BaseResponse.success( userService.checkBalance(reqForm));
    }

    /**
     * 获取用户信息
     * @param userCode
     * @return
     */
    @GetMapping("/{userCode}/getUserInfo")
    public BaseResponse<MyUserInfo> getUserInfo(@PathVariable String userCode) throws BusinessException {
        return BaseResponse.success(userService.getUserInfo(userCode));
    }

    /**
     * 获取用户车牌
     * @param userCode
     * @return
     */
    @GetMapping("/{userCode}/getCarNoList")
    public BaseResponse<List<UserVehicleDto>> getCarNoList(@PathVariable String userCode){
        return BaseResponse.success(userService.getCarNoList(userCode));
    }

    /**
     * 获取用户消费明细
     * @param reqForm
     * @return
     */
    @PostMapping("/getUserBalanceRecords")
    public BaseResponse<List<UserBalanceRecordDto>> getUserBalanceRecords(@Validated @RequestBody UserBalanceRecordReqForm reqForm){
        ApiResult<List<UserBalanceRecordDto>> apiResult=userService.getUserBalanceRecords(reqForm);
        return BaseResponse.success(apiResult.getResult(),apiResult.getTotal());

    }
}
